package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.PackagePostageModel;
import com.btkjgc.service.intf.PackageFareManageService_Intf;

@Controller
public class PackageFareManageController
{
	static Logger logger = Logger.getLogger(PackageFareManageController.class);
	
	@Autowired
	private PackageFareManageService_Intf packageFareManageService;
	
	@RequestMapping(value = "package_fare_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,PackagePostageModel packagePostageModel)
	{
		ModelAndView mv = new ModelAndView("package_fare_manage");
		listrecord(request,mv,packagePostageModel);
		return mv;
	}
	
	@RequestMapping(value = "view_package_fare", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView viewPckageFare(HttpServletRequest request,PackagePostageModel packagePostageModel)
	{
		 ModelAndView mv = new ModelAndView("add_package_fare");
		 
		 if ("edit".equals(packagePostageModel.getTask())) {
			 packageFareManageService.setEditdata(request,mv,packagePostageModel.getId());
		 }
		
		 return mv;
	}

	@SuppressWarnings("unchecked")
	private void listrecord(HttpServletRequest request, ModelAndView mv,PackagePostageModel packagePostageModel){
 
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if(StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}
		
		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null)
		{
			pageIndex = Integer.parseInt(page);
		}
		
		List<Object[]> paramList = new ArrayList<Object[]>();
		String[] arr = { "name","like", packagePostageModel.getName()};
		paramList.add(arr);
		
		String sql = "SELECT\n" +
					 "ppt.*, aut.username,\n" +
					 "case \n" +
					 "when ppt.endTime is null then '不限'\n" +
					 "ELSE ppt.endTime\n" +
					 "end showEndTime,\n" +
					 "case  \n" +
					 "when ppt.beginTime is null then '即时生效'\n" +
					 "ELSE   ppt.beginTime\n" +
					 "end showBeginTime,\n" +
					 "case type\n" +
					 "when 1  then '里程'\n" +
					 "when 2  then '次数'\n" +
					 "end typeName,\n" +
					 "case status\n" +
					 "when 1  then '开启'\n" +
					 "when 2  then '暂停'\n" +
					 "end statusName,\n" +
					 "case type\n" +
					 "when 1  then '元/公里'\n" +
					 "when 2  then '元/次'\n" +
					 "end unitName\n" +
					 "FROM\n" +
					 "	package_postage_t ppt\n" +
					 "LEFT JOIN admin_user_t aut ON aut.id = ppt.operid\n" +
					 "WHERE\n" +
					 "	ppt.del = 0 ";
		
		sql = packageFareManageService.getwhereSQL(sql, paramList, sortfield);
		
		totalRecord = packageFareManageService.getCount(sql, paramList);
		
		// 获取分页查询的数据结果
		List<PackagePostageModel> packagePostageList = packageFareManageService.getList(
				PackagePostageModel.class, paramList, sql, pageIndex, 15, sortfield);
		
		// 设置后台用户界面所需的其他数据
		packageFareManageService.setListData(request, mv, pageIndex, totalRecord,
				packagePostageList);
		
	}
	
	
	@RequestMapping(value = "edit_package_fare", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public AjaxObj editPackFare(PackagePostageModel packagePostageModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {

		AjaxObj ajaxObj=packageFareManageService.updatePackagePostageModel(packagePostageModel);

		return ajaxObj;
	}

	
	@RequestMapping(value = "batchdel_package_fare", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String batchdelConventionalFare(HttpServletRequest request, String idStr) {
 
	    packageFareManageService.updateDelByIds(idStr);
 
		return "redirect:/package_fare_manage";
	}
	
	@RequestMapping(value = "batchpause_package_fare", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String batchpauseConventionalFare(HttpServletRequest request, String idStr) {
 
		packageFareManageService.updateStateByIds(idStr,2);
	 
		return "redirect:/package_fare_manage";
	}
	
	@RequestMapping(value = "batchstart_package_fare", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String batchstartConventionalFare(HttpServletRequest request, String idStr) {
 
	    packageFareManageService.updateStateByIds(idStr,1);
	 
		return "redirect:/package_fare_manage";
	}
	
 
	

	@RequestMapping(value = "add_package_postage/{a}")
	@ResponseBody
	public   AjaxObj jsonAddPackagePostage(HttpServletRequest request,
			@PathVariable("a") int a ,PackagePostageModel packagePostageModel) {
 
 
		  AjaxObj ajaxObj = packageFareManageService
					.insertPackageFareModel(packagePostageModel);
 
		return ajaxObj;
	}
	
	

	
}